package com.hanlink.dsp.ca.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.ViewPager;

import com.edge.pbx.view.PbxCircleSeekBar;
import com.edge.pbx.view.common.ChNameRadioView;
import com.edge.pbx.view.eq.EqChannelView.OnSeekBarChangeListener;
import com.edge.pbx.view.eq.IndicatorView;
import com.edge.pbx.view.summing.PbxOutputSelectChannelView;
import com.edge.pbx.view.summing.PbxOutputSelectChannelsLayout;
import com.edge.pbxdsp.R;
import com.hanlink.dsp.BtDspApplication;
import com.hanlink.dsp.DspSettings;

import com.hanlink.dsp.ca.model.channel.EqChannel;
import com.hanlink.dsp.ca.model.channel.XoverChannel;
import com.hanlink.dsp.ca.ui.base.BaseAppFragment;
import com.hanlink.dsp.protocols.DspProtocol;
import com.hanlink.dsp.ui.activities.HomeViewModel;
import com.hanlink.dsp.ui.adapter.ViewPagerAdapter;
import com.hanlink.dsp.ui.dialog.EffectDialog;
import com.hanlink.dsp.ui.dialog.GroupingDialog;
import com.edge.pbx.view.eq.EqChannelView;
import com.hanlink.dsp.ui.widget.XoverEditText;
import com.hanlink.dsp.ui.widget.chart.EqView;
import com.hanlink.dsp.util.DataUtil;
import com.hanlink.dsp.util.DrawFunction;
import com.hanlink.dsp.util.DspUtil;
import com.hanlink.dsp.util.FreqUtil;
import com.hanlink.dsp.util.ViewUtil;
import com.qztech.sdk.model.Grouping;
import com.qztech.sdk.model.SettingConstants;
import com.qztech.utils.YzDebug;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

//import butterknife.BindView;
//import butterknife.BindViews;
//import butterknife.OnClick;
//import butterknife.OnLongClick;

import static com.hanlink.dsp.DspSettings.EQ_SETTINGS;
import static com.hanlink.dsp.DspSettings.XOVER_SETTINGS;
import static com.hanlink.dsp.DspSettings.isSupportGroupAll;

@SuppressWarnings("unused")
public class EqFragment extends BaseAppFragment implements EffectDialog.IRefreshListener,
        GroupingDialog.OnGroupingChangedListener {

//    @BindViews({R.id.rbBtnCh1, R.id.rbBtnCh2, R.id.rbBtnCh3, R.id.rbBtnCh4, R.id.rbBtnCh5, R.id.rbBtnCh6, R.id.rbBtnCen, R.id.rbBtnSub})
//    ClickableCheckBox[] cbxChannels;


    //@BindView(R.id.pbx_eq_et_freq)
    XoverEditText mEtFreq;
    //@BindView(R.id.pbx_eq_et_q)
    XoverEditText mEtQ;
    //@BindView(R.id.btn_reset)
    ImageButton mBtnReset; // Reset button
    //@BindView(R.id.btn_effect)
    Button mBtnEffect; //effect button
    //@BindView(R.id.esv_eqchart)
    EqView mEqChart;
    private ChNameRadioView cbGroup1,cbGroup2,cbGroup3,cbGroup4;
    //@BindViews({R.id.cbGroup1, R.id.cbGroup2, R.id.cbGroup3, R.id.cbGroup4})
    ChNameRadioView[] mCbGroup;
    //@BindView(R.id.read)
    TextView mTvRead;

    //	@BindViews({R.id.eqc_1, R.id.eqc_2, R.id.eqc_3, R.id.eqc_4, R.id.eqc_5, R.id.eqc_6, R.id.eqc_7, R.id.eqc_8})
    EqChannelView[] mEqItemViews = new EqChannelView[DspSettings.FRENCY_LENGTH];

    //@BindView(R.id.vpChannels)
    ViewPager mVpChannelsPager;

    //@BindView(R.id.idv_eq)
    IndicatorView mIndicatorView;
    //
   // @BindView(R.id.pbx_output_selects_layout)
    PbxOutputSelectChannelsLayout mPbxOutputSelectChannelsLayout;
    final List<PbxOutputSelectChannelView> mOutputViews = new ArrayList<>(8);

    //@BindView(R.id.eq_seekbar_q)
    PbxCircleSeekBar mSeekBarQ;
    //@BindView(R.id.eq_seekbar_freq)
    PbxCircleSeekBar mSeekBarFreq;
    //
    //@BindView(R.id.btn_eq_default)
    ImageButton mIvEqDefault;
    private boolean isUpdated = false; //是否更新了数据
    private boolean isReset = false;
//    private int mCurrentChlIndex = 0;
    private int mCurrentFreqIndex;
    private GroupingDialog mGroupingDialog;
    private float[] chartQ;
    private int[] chartGain;
    private HomeViewModel mHomeViewModel;

    @Override
    protected int getResourceId() {
        return R.layout.fragment_eq;
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mEtFreq = findViewById(R.id.pbx_eq_et_freq);
        mEtQ = findViewById(R.id.pbx_eq_et_q);
        mBtnReset = findViewById(R.id.btn_reset);
        mBtnEffect = findViewById(R.id.btn_effect);
        mEqChart = findViewById(R.id.esv_eqchart);
        cbGroup1 = findViewById(R.id.cbGroup1);
        cbGroup2 = findViewById(R.id.cbGroup2);
        cbGroup3 = findViewById(R.id.cbGroup3);
        cbGroup4 = findViewById(R.id.cbGroup4);
        mCbGroup = new ChNameRadioView[]{cbGroup1,cbGroup2,cbGroup3,cbGroup4};

        for(ChNameRadioView cb : mCbGroup){
            cb.setOnClickListener(onGroupClick);
            cb.setOnLongClickListener(onGroupLongClick);
        }

        mTvRead = findViewById(R.id.read);
        mVpChannelsPager = findViewById(R.id.vpChannels);
        mIndicatorView = findViewById(R.id.idv_eq);
        mPbxOutputSelectChannelsLayout = findViewById(R.id.pbx_output_selects_layout);
        mSeekBarQ = findViewById(R.id.eq_seekbar_q);
        mSeekBarFreq = findViewById(R.id.eq_seekbar_freq);
        mIvEqDefault = findViewById(R.id.btn_eq_default);

        mBtnEffect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EffectDialog dialog = new EffectDialog(getActivity(), EqFragment.this);
                dialog.setRefreshListener(EqFragment.this);
                dialog.show();
            }
        });

        mBtnReset.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                isReset = true;
                DspSettings.EQ_SETTINGS.reset();

                // DspSettings.EQ_SETTINGS.setLastChannel(0);
                updateData();
                DataUtil.saveEqSettingsToDb();
                return true;
            }
        });

        mIvEqDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                YzDebug.d("-----------------onDefaultButtonCheckedChanged---------------");
                boolean selected = !view.isSelected();
                view.setSelected(selected);
                updateEqItemViewEnable(!selected);
                for (PbxOutputSelectChannelView outputView : mOutputViews) {
                    outputView.setEnabled(!selected);
                }

                for (View group : mCbGroup) {
                    group.setEnabled(!selected);
                }
                mSeekBarFreq.setEnabled(!selected);
                mSeekBarQ.setEnabled(!selected);
                mBtnReset.setEnabled(!selected);
                mBtnEffect.setEnabled(!selected);
                mEtQ.setEnabled(!selected);
                mEtFreq.setEnabled(!selected);
            }
        });

        findViewById(R.id.eq_page_sub).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int cur = mVpChannelsPager.getCurrentItem();
                if (++cur <= mVpChannelsPager.getChildCount()){
                    mVpChannelsPager.setCurrentItem(cur);
                }
            }
        });

        findViewById(R.id.eq_page_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int cur = mVpChannelsPager.getCurrentItem();
                cur = mVpChannelsPager.getCurrentItem();
                if (--cur >= 0){
                    mVpChannelsPager.setCurrentItem(cur);
                }
            }
        });

        onResumeTime = SystemClock.elapsedRealtime();
        YzDebug.d("Yang", "---onViewCreated---");
        ViewPagerAdapter adapter = new ViewPagerAdapter();
        mIndicatorView.setViewPager(mVpChannelsPager);
        ViewUtil.findOutputViews(mOutputViews, view, mChannelSelectListener);

        mVpChannelsPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                DspSettings.CURRENT_EQ_INDEX = position;
                initFreqAndQValue(position, mCurrentFreqIndex);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
//        EqChannel channel = DspSettings.EQ_SETTINGS.getEqChannels().get(EqFragment.this.mCurrentChlIndex);
        createViewPagerView(adapter);
        mVpChannelsPager.setAdapter(adapter);
        mGroupingDialog = new GroupingDialog(getActivity());
        mGroupingDialog.setGroupingChangedListener(this);
        mHomeViewModel = ViewModelProviders.of(getActivity(), new ViewModelProvider.AndroidViewModelFactory(BtDspApplication.getInstance())).get(HomeViewModel.class);
        mSeekBarFreq.setOnCircleSeekBarListener(mOnFreqSeekBarListener);
        mSeekBarFreq.setOnCircleSeekBarFunListener(mOnCircleSeekBarFunListener);
        mSeekBarQ.setOnCircleSeekBarListener(mOnQSeekBarListener);
        mSeekBarQ.setOnCircleSeekBarFunListener(mOnCircleSeekBarFunListener);
        mEtFreq.setValidEditTextListener(mEditTextListener);
        mEtQ.setValidEditTextListener(mEditTextListener);
        ViewUtil.initChannelName(mOutputViews);
    }

    private void createViewPagerView(ViewPagerAdapter adapter) {
        final int layoutId = R.layout.layout_eq_channel_page;
        final int pageSize = 8;
        int[] CHANNEL_VALUE = DspProtocol.getDefaultChannelValues();
//        final int ids[] = {R.id.eqc_item_1, R.id.eqc_item_2, R.id.eqc_item_3, R.id.eqc_item_4,R.id.eqc_item_5,R.id.eqc_item_6,R.id.eqc_item_7,R.id.eqc_item_8,};
        final int ids[] = {R.id.eqc_1, R.id.eqc_2, R.id.eqc_3, R.id.eqc_4, R.id.eqc_5, R.id.eqc_6, R.id.eqc_7, R.id.eqc_8,};
        ArrayList<View> views = new ArrayList<>();
        double d = (float) CHANNEL_VALUE.length / pageSize;

        for (int i = 0, size = (int) Math.ceil(d); i < size; i++) {
            views.add(LayoutInflater.from(getActivity()).inflate(layoutId, mVpChannelsPager, false));
        }
        View rootView = null;
        EqChannelView v = null;
        int index = 0;
        for (int pageIndex = 0, size = views.size(); pageIndex < size; pageIndex++) {
            rootView = views.get(pageIndex);
            for (int i = 0; i < pageSize; i++) {
                index = pageIndex * pageSize + i;
                v = rootView.findViewById(ids[i]);
                if (index < CHANNEL_VALUE.length) {
                    v.setPageAndIndex(pageIndex, index);
                    v.setFreqValue(CHANNEL_VALUE[index]);
                    v.getVerticalSeekBar().setParentViewPager(mVpChannelsPager);
                    v.setOnSeekBarChangeListener(mOnGainSeekBarChangeListener);
//                    YzDebug.d("Yang" ," createViewPagerView:"+ i +" " + pageIndex +" " + index);
                    mEqItemViews[index] = v;
                    v.setVisibility(View.VISIBLE);
                } else {
                    v.setVisibility(View.GONE);
                }

            }
        }

        adapter.setViews(views);
    }


    private void updateData() {
        isReset = true;
        try {
            updateEffectText();
            mCurrentFreqIndex = 0;
            ViewUtil.initOutputChannels(DspSettings.SUMMING_SETTINGS, mOutputViews);
            setGroupState();
            resetSelectedChannelStatus();
            List<EqChannel> eqChannels = DspSettings.EQ_SETTINGS.getEqChannels();
            int[] gain = eqChannels.get(DspSettings.EQ_SETTINGS.getLastChannel()).getGain();
//            float[] q = eqChannels.get(DspSettings.EQ_SETTINGS.getLastChannel()).getQ();
            int[] freq = eqChannels.get(DspSettings.EQ_SETTINGS.getLastChannel()).getFreq();


            isUpdated = true;
            for (int i = 0, len = mEqItemViews.length; i < len; i++) {
                mEqItemViews[i].setGainValue(gain[i]);
                mEqItemViews[i].setFreqValue(freq[i]);
            }

            createChart(eqChannels.get(DspSettings.EQ_SETTINGS.getLastChannel()));


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            isUpdated = false;
            mEqChart.postDelayed(() -> isReset = false, 300);
        }
    }


    private void setGroupState() {
        mCbGroup[0].setSelected(DspSettings.EQ_SETTINGS.isGroup12());
        mCbGroup[1].setSelected(DspSettings.EQ_SETTINGS.isGroup34());
        mCbGroup[2].setSelected(DspSettings.EQ_SETTINGS.isGroup56());
        mCbGroup[3].setSelected(DspSettings.EQ_SETTINGS.isGroup78());
    }

    private long onResumeTime = 0;
    @Override
    public void onResume() {
        super.onResume();
        ViewUtil.initChannelName(mOutputViews,mCbGroup);

        ViewUtil.initCarType(mPbxOutputSelectChannelsLayout);
        onResumeTime = SystemClock.elapsedRealtime();
        updateData(); //加载数据

    }

    @Override
    public void onStop() {
        if (mGroupingDialog != null && mGroupingDialog.isShowing()) {
            mGroupingDialog.dismiss();
        }
        super.onStop();
    }

    @Override
    public void refreshActivity(int value) {

        DataUtil.setupPreEffetData(value);
        EQ_SETTINGS.setEffect(value+1);
        updateEffectText();
        mHomeViewModel.startUpload();
        updateData();

    }
    private void updateEffectText(){
        int value = EQ_SETTINGS.getEffect();
        YzDebug.d("updateEffectText:"+ value);
        if(value == 0){
            mBtnEffect.setText(com.hanlink.res.R.string.eq_effect);
        }else {
            String[] mItems = getContext().getResources().getStringArray(R.array.effect_items);
            mBtnEffect.setText(String.format("Preset(%s)", mItems[value-1]));
        }

    }
    private int findCheckedIndex(int groupChannel) {
        if (DspSettings.EQ_SETTINGS.isGroupAll()) {
            final int[] info = DspSettings.EQ_SETTINGS.getGroupInfo();
            for (int i = 0; i < info.length; i++) {
                if (info[i] == 1111)
                    return i;
            }
        }
        switch (groupChannel) {
            case 0:
            case 1:
                if (DspSettings.EQ_SETTINGS.isGroup12()) {
                    return DspSettings.EQ_SETTINGS.getBaseCh1() == 1 ? 0 : 1;
                } else return groupChannel;
            case 2:
            case 3:
                if (DspSettings.EQ_SETTINGS.isGroup34()) {
                    return DspSettings.EQ_SETTINGS.getBaseCh3() == 1 ? 2 : 3;
                } else return groupChannel;
            case 4:
            case 5:
                if (DspSettings.EQ_SETTINGS.isGroup56()) {
                    return DspSettings.EQ_SETTINGS.getBaseCh5() == 1 ? 4 : 5;
                } else return groupChannel;
            case 6:
            case 7:
                if (DspSettings.EQ_SETTINGS.isGroup78()) {
                    return DspSettings.EQ_SETTINGS.getBaseCh7() == 1 ? 6 : 7;
                } else return groupChannel;
        }
        return groupChannel;
    }





    /**
     * 重置CH1-CH6选中状态
     */
    @SuppressLint("NewApi")
    private void resetSelectedChannelStatus() {
        int index = findCheckedIndex(DspSettings.EQ_SETTINGS.getLastChannel());
        resetSelectedChannelStatus(index);
    }

    /**
     * 重置CH1-CH6选中状态
     */
    @SuppressLint("NewApi")
    private void resetSelectedChannelStatus(int baseChannel) {
        YzDebug.d("resetSelectedChannelStatus==============:" + baseChannel);
        for (int j = 0, len = mOutputViews.size(); j < len; j++) {
            mOutputViews.get(j).setSelected(false);
        }
        if (DspSettings.EQ_SETTINGS.isGroup12() && (baseChannel == 0 || baseChannel == 1)) {
            mOutputViews.get(0).setSelected(true);
            mOutputViews.get(1).setSelected(true);
        } else if (DspSettings.EQ_SETTINGS.isGroup34() && (baseChannel == 2 || baseChannel == 3)) {
            mOutputViews.get(2).setSelected(true);
            mOutputViews.get(3).setSelected(true);
        } else if (DspSettings.EQ_SETTINGS.isGroup56() && (baseChannel == 4 || baseChannel == 5)) {
            mOutputViews.get(4).setSelected(true);
            mOutputViews.get(5).setSelected(true);
        } else if (DspSettings.EQ_SETTINGS.isGroup78() && (baseChannel == 6 || baseChannel == 7)) {
            mOutputViews.get(6).setSelected(true);
            mOutputViews.get(7).setSelected(true);
        } else if (DspSettings.EQ_SETTINGS.isGroupAll()) {
            for (PbxOutputSelectChannelView channel : mOutputViews) {
                channel.setSelected(true);
            }
        } else {
            mOutputViews.get(baseChannel).setSelected(true);
        }
        onChannelChanged(baseChannel);
    }

    //EQ default
//    @OnCheckedChanged(R.id.btn_eq_default)
//    @OnClick(R.id.btn_eq_default)
////    public void onDefaultButtonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//    public void onDefaultButtonCheckedChanged(View view) {
//        YzDebug.d("-----------------onDefaultButtonCheckedChanged---------------");
//        boolean selected = !view.isSelected();
//        view.setSelected(selected);
//        updateEqItemViewEnable(!selected);
//        for (PbxOutputSelectChannelView outputView : mOutputViews) {
//            outputView.setEnabled(!selected);
//        }
//
//        for (View group : mCbGroup) {
//            group.setEnabled(!selected);
//        }
//        mSeekBarFreq.setEnabled(!selected);
//        mSeekBarQ.setEnabled(!selected);
//        mBtnReset.setEnabled(!selected);
//        mBtnEffect.setEnabled(!selected);
//        mEtQ.setEnabled(!selected);
//        mEtFreq.setEnabled(!selected);
//    }


    private View.OnClickListener onGroupClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            Grouping index = Grouping.UNKNOWN;
            switch (id) {
                case R.id.cbGroup1:
                    index = Grouping.GROUP1_2;
                    if (DspSettings.EQ_SETTINGS.isGroup12()) return;
                    break;
                case R.id.cbGroup2:
                    index = Grouping.GROUP3_4;
                    if (DspSettings.EQ_SETTINGS.isGroup34()) return;
                    break;
                case R.id.cbGroup3:
                    index = Grouping.GROUP5_6;
                    if (DspSettings.EQ_SETTINGS.isGroup56()) return;
                    break;
                case R.id.cbGroup4:
                    if (isSupportGroupAll) {
                        index = Grouping.GROUP_ALL;
                        if (DspSettings.EQ_SETTINGS.isGroupAll()) return;
                    } else {
                        index = Grouping.GROUP7_8;
                        if (DspSettings.EQ_SETTINGS.isGroup78()) return;
                    }

                    // index = Grouping.GROUP7_8;
                    // if (DspSettings.EQ_SETTINGS.isGroup78()) return;
                    break;
            }
            YzDebug.d("onGroupClick:" + index);
            mGroupingDialog.setSelectGroupIndex(index);
            mGroupingDialog.show();
        }
    };


    private View.OnLongClickListener onGroupLongClick = new View.OnLongClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean onLongClick(View v) {
            switch (v.getId()) {
                case R.id.cbGroup1:
                    if (DspSettings.EQ_SETTINGS.isGroup12()) {
                        mCbGroup[0].setSelected(false);
                        if (DspSettings.EQ_SETTINGS.getBaseCh1() == 1) {
                            mOutputViews.get(1).setSelected(false);
                            DspSettings.EQ_SETTINGS.setBaseCh1(0);
                            DspSettings.EQ_SETTINGS.setBaseCh2(0);
                        } else if (DspSettings.EQ_SETTINGS.getBaseCh2() == 1) {
                            mOutputViews.get(0).setSelected(false);
                            DspSettings.EQ_SETTINGS.setBaseCh1(0);
                            DspSettings.EQ_SETTINGS.setBaseCh2(0);
                        }
                    }
                    break;
                case R.id.cbGroup2:
                    if (DspSettings.EQ_SETTINGS.isGroup34()) {
                        mCbGroup[1].setSelected(false);
                        if (DspSettings.EQ_SETTINGS.getBaseCh3() == 1) {
                            mOutputViews.get(3).setSelected(false);
                            DspSettings.EQ_SETTINGS.setBaseCh3(0);
                            DspSettings.EQ_SETTINGS.setBaseCh4(0);
                        } else if (DspSettings.EQ_SETTINGS.getBaseCh4() == 1) {
                            mOutputViews.get(2).setSelected(false);
                            DspSettings.EQ_SETTINGS.setBaseCh3(0);
                            DspSettings.EQ_SETTINGS.setBaseCh4(0);
                        }
                    }
                    break;
                case R.id.cbGroup3:
                    if (DspSettings.EQ_SETTINGS.isGroup56()) {
                        mCbGroup[2].setSelected(false);
                        if (DspSettings.EQ_SETTINGS.getBaseCh5() == 1) {
                            mOutputViews.get(5).setSelected(false);
                            DspSettings.EQ_SETTINGS.setBaseCh5(0);
                            DspSettings.EQ_SETTINGS.setBaseCh6(0);
                        } else if (DspSettings.EQ_SETTINGS.getBaseCh6() == 1) {
                            mOutputViews.get(4).setSelected(false);
                            DspSettings.EQ_SETTINGS.setBaseCh5(0);
                            DspSettings.EQ_SETTINGS.setBaseCh6(0);
                        }
                    }
                    break;
                case R.id.cbGroup4:
                    mCbGroup[3].setSelected(false);
                    if (isSupportGroupAll) {
                        final int[] groupInfo = DspSettings.EQ_SETTINGS.getGroupInfo();
                        for (int i = 0; i < groupInfo.length; i++) {
                            mOutputViews.get(i).setSelected(groupInfo[i] == 1111);
                        }
                        Arrays.fill(groupInfo, 0);
                        DspSettings.EQ_SETTINGS.setBaseChs(groupInfo);
                    } else {
                        if (DspSettings.EQ_SETTINGS.isGroup78()) {
                            if (DspSettings.EQ_SETTINGS.getBaseCh7() == 1) {
                                mOutputViews.get(7).setSelected(false);
                                DspSettings.EQ_SETTINGS.setBaseCh7(0);
                                DspSettings.EQ_SETTINGS.setBaseCh8(0);
                            } else if (DspSettings.EQ_SETTINGS.getBaseCh8() == 1) {
                                mOutputViews.get(6).setSelected(false);
                                DspSettings.EQ_SETTINGS.setBaseCh7(0);
                                DspSettings.EQ_SETTINGS.setBaseCh8(0);
                            }
                        }
                    }
            }

            return false;
        }
    };

   /* @OnClick({R.id.cbGroup1, R.id.cbGroup2, R.id.cbGroup3, R.id.cbGroup4})
    public void onGroupClick(View view) {

        Grouping index = Grouping.UNKNOWN;
        switch (view.getId()) {
            case R.id.cbGroup1:
                index = Grouping.GROUP1_2;
                if (DspSettings.EQ_SETTINGS.isGroup12()) return;
                break;
            case R.id.cbGroup2:
                index = Grouping.GROUP3_4;
                if (DspSettings.EQ_SETTINGS.isGroup34()) return;
                break;
            case R.id.cbGroup3:
                index = Grouping.GROUP5_6;
                if (DspSettings.EQ_SETTINGS.isGroup56()) return;
                break;
            case R.id.cbGroup4:
                if (isSupportGroupAll) {
                    index = Grouping.GROUP_ALL;
                    if (DspSettings.EQ_SETTINGS.isGroupAll()) return;
                } else {
                    index = Grouping.GROUP7_8;
                    if (DspSettings.EQ_SETTINGS.isGroup78()) return;
                }

                // index = Grouping.GROUP7_8;
                // if (DspSettings.EQ_SETTINGS.isGroup78()) return;
                break;
        }
        YzDebug.d("onGroupClick:" + index);
        mGroupingDialog.setSelectGroupIndex(index);
        mGroupingDialog.show();
    }*/

    //yz use
/*    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @OnLongClick({R.id.cbGroup1, R.id.cbGroup2, R.id.cbGroup3, R.id.cbGroup4})
    public boolean onGroupLongClick(View view) {
        switch (view.getId()) {
            case R.id.cbGroup1:
                if (DspSettings.EQ_SETTINGS.isGroup12()) {
                    mCbGroup[0].setSelected(false);
                    if (DspSettings.EQ_SETTINGS.getBaseCh1() == 1) {
                        mOutputViews.get(1).setSelected(false);
                        DspSettings.EQ_SETTINGS.setBaseCh1(0);
                        DspSettings.EQ_SETTINGS.setBaseCh2(0);
                    } else if (DspSettings.EQ_SETTINGS.getBaseCh2() == 1) {
                        mOutputViews.get(0).setSelected(false);
                        DspSettings.EQ_SETTINGS.setBaseCh1(0);
                        DspSettings.EQ_SETTINGS.setBaseCh2(0);
                    }
                }
                break;
            case R.id.cbGroup2:
                if (DspSettings.EQ_SETTINGS.isGroup34()) {
                    mCbGroup[1].setSelected(false);
                    if (DspSettings.EQ_SETTINGS.getBaseCh3() == 1) {
                        mOutputViews.get(3).setSelected(false);
                        DspSettings.EQ_SETTINGS.setBaseCh3(0);
                        DspSettings.EQ_SETTINGS.setBaseCh4(0);
                    } else if (DspSettings.EQ_SETTINGS.getBaseCh4() == 1) {
                        mOutputViews.get(2).setSelected(false);
                        DspSettings.EQ_SETTINGS.setBaseCh3(0);
                        DspSettings.EQ_SETTINGS.setBaseCh4(0);
                    }
                }
                break;
            case R.id.cbGroup3:
                if (DspSettings.EQ_SETTINGS.isGroup56()) {
                    mCbGroup[2].setSelected(false);
                    if (DspSettings.EQ_SETTINGS.getBaseCh5() == 1) {
                        mOutputViews.get(5).setSelected(false);
                        DspSettings.EQ_SETTINGS.setBaseCh5(0);
                        DspSettings.EQ_SETTINGS.setBaseCh6(0);
                    } else if (DspSettings.EQ_SETTINGS.getBaseCh6() == 1) {
                        mOutputViews.get(4).setSelected(false);
                        DspSettings.EQ_SETTINGS.setBaseCh5(0);
                        DspSettings.EQ_SETTINGS.setBaseCh6(0);
                    }
                }
                break;
            case R.id.cbGroup4:
                mCbGroup[3].setSelected(false);
                if (isSupportGroupAll) {
                    final int[] groupInfo = DspSettings.EQ_SETTINGS.getGroupInfo();
                    for (int i = 0; i < groupInfo.length; i++) {
                        mOutputViews.get(i).setSelected(groupInfo[i] == 1111);
                    }
                    Arrays.fill(groupInfo, 0);
                    DspSettings.EQ_SETTINGS.setBaseChs(groupInfo);
                } else {
                    if (DspSettings.EQ_SETTINGS.isGroup78()) {
                        if (DspSettings.EQ_SETTINGS.getBaseCh7() == 1) {
                            mOutputViews.get(7).setSelected(false);
                            DspSettings.EQ_SETTINGS.setBaseCh7(0);
                            DspSettings.EQ_SETTINGS.setBaseCh8(0);
                        } else if (DspSettings.EQ_SETTINGS.getBaseCh8() == 1) {
                            mOutputViews.get(6).setSelected(false);
                            DspSettings.EQ_SETTINGS.setBaseCh7(0);
                            DspSettings.EQ_SETTINGS.setBaseCh8(0);
                        }
                    }
                }
                break;
        }
        return true;
    }*/

//    @OnClick({R.id.btn_effect})
//    public void onOtherButtonClick(View v) {
//        if (v.getId() == R.id.btn_effect) {
//            EffectDialog dialog = new EffectDialog(getActivity(), EqFragment.this);
//            dialog.setRefreshListener(EqFragment.this);
//            dialog.show();
//        }
//    }

//    @OnLongClick(R.id.btn_reset)
//    public boolean onReset(View view) {
//        isReset = true;
//        DspSettings.EQ_SETTINGS.reset();
//
//        // DspSettings.EQ_SETTINGS.setLastChannel(0);
//        updateData();
//        DataUtil.saveEqSettingsToDb();
//        return true;
//    }

    private void onChannelChanged(int index) {
        DspSettings.EQ_SETTINGS.setLastChannel(index);

        EqChannel curChannel = DspSettings.EQ_SETTINGS.getEqChannels().get( EQ_SETTINGS.getLastChannel());
        createChart(curChannel);
        int i = 0;
        YzDebug.d("onChannelChanged mCurrentChlIndex:" +  EQ_SETTINGS.getLastChannel());
        isUpdated = true;
        for (EqChannelView view : mEqItemViews) {
            view.setEnabled(true);
            view.setGainValue(curChannel.getGainValue(i));
//            view.setSelected(i == mCurrentFreqIndex);
            view.setFreqValue(curChannel.getFreqValue(i));
//            view.setValue(curChannel.getQValue(i), curChannel.getGainValue(i),curChannel.getFreqValue(i));
            i++;
        }
        updateEqItemsState(0,mCurrentFreqIndex);
//        mSeekBarQ.setProgress(DspUtil.qToProcess(curChannel.getQValue(mCurrentFreqIndex)));
//        mSeekBarFreq.setProgress(FreqUtil.parseFreqToProgress(curChannel.getFreqValue(mCurrentFreqIndex)));
        updateEqItemViewEnable(!mIvEqDefault.isSelected());
        isUpdated = false;
    }

    //设置其他项为非选中状态
    private void updateEqItemsState(int page, int index) {


        for (int i = 0, len = mEqItemViews.length; i < len; i++) {
            if (i != index) {
                mEqItemViews[i].setSelected(false);
//                mRbFreqValues[i].setChecked(false);
            } else {
                mEqItemViews[i].setSelected(true);
//                mRbFreqValues[i].setChecked(true);
            }
        }
        EqChannel channel = EQ_SETTINGS.getEqChannels().get( EQ_SETTINGS.getLastChannel());
        boolean anim = SystemClock.elapsedRealtime() - onResumeTime > 2000;
        float qValue = channel.getQValue(index);
        int freqValue = channel.getFreqValue(index);


        mSeekBarQ.setProgress(DspUtil.qToProcess(qValue) ,anim);
        mEtQ.setText(String.format("%.02f", qValue/100));

        mSeekBarFreq.setProgress(FreqUtil.parseFreqToProgress(freqValue),anim);
        mEtFreq.setText(String.valueOf(freqValue));

    }

    /**
     * Q值和Freg 选中回调
     */
//    private EqChannelView.OnCheckChangedCallback mOnCheckChangedCallback = (page, freqIndex, isChecked) -> {
//        if (!isChecked) return;
//        initFreqAndQValue(page, freqIndex);
//    };
    private void initFreqAndQValue(int page, int freqIndex) {


        if (freqIndex < 0 || freqIndex >=  mEqItemViews.length){
            mCurrentFreqIndex = 0;
            updateEqItemsState(page, mCurrentFreqIndex);
        }else {
            if (mCurrentFreqIndex != freqIndex){
                YzDebug.d("initFreqAndQValue:" + freqIndex);
                mCurrentFreqIndex = freqIndex;//0-31
                updateEqItemsState(page, freqIndex);
            }
        }

        //reset EqchannerView


    }


    @Override
    public void onRefreshFragment(int cmd) {
        if (cmd == CMD_SELECTED) {
            updateData();
        }
    }

    //aa55abc300001400c80190000000

    @Override
    public void onGrouping(Grouping groupIndex, int groupChannel) {
        int max = 8;
        YzDebug.d("onGrouping:" + groupIndex + " " + groupChannel);
        boolean enabled = false;
        if (groupChannel >= 0 && groupChannel < max) {
            int[] baseChValues = new int[max];
            switch (groupIndex) {
                case GROUP_ALL:
                    enabled = true;
                    baseChValues[groupChannel] = 1111;
                    break;
                case GROUP1_2:
                case GROUP3_4:
                case GROUP5_6:
                case GROUP7_8:
                    enabled = true;
                    if (DspSettings.EQ_SETTINGS.isGroupAll()) {
                        mCbGroup[3].setSelected(false);
                        Arrays.fill(baseChValues, 0);
                    } else {
                        Arrays.fill(baseChValues, -1);
                    }
                    baseChValues[groupChannel] = 1;
                    break;
            }
            if (enabled) {
                DspSettings.EQ_SETTINGS.setBaseChs(baseChValues);
                syncGroupChannel(groupChannel, groupIndex.getGroupIndexs());
            }
            resetSelectedChannelStatus(groupChannel);
            DspSettings.EQ_SETTINGS.getEqChannels().get(groupChannel).sendAllCmdOnNewThread(true);
        } else {
            resetSelectedChannelStatus();
        }
        setGroupState();
    }

    private void syncGroupChannel(int baseChannel, int[] channels) {
        List<EqChannel> eqChannels = DspSettings.EQ_SETTINGS.getEqChannels();
        EqChannel baseEqChannel = eqChannels.get(baseChannel);

        for (int channel : channels) {
            if (baseChannel == channel) continue;
            if (channel >= eqChannels.size()) continue;
            eqChannels.get(channel).syncGroupData(baseEqChannel);
        }
    }

    private void createChart(EqChannel eqChannel) {
        int qSize = eqChannel.getQ().length;
        int gainSize = eqChannel.getGain().length;
        float[] chartQ = new float[qSize];
        int[] chartGain = new int[gainSize];
        for (int i = 0; i < qSize; i++) {
            chartQ[i] = eqChannel.getQ()[i] / 100f;
            //yang   gain 从-200~+200 改为 -120~+120
            chartGain[i] = (eqChannel.getGain()[i] + SettingConstants.GAIN_MIN) / 10;
//            chartGain[i] = (eqChannel.getGain()[i] - 200) / 10;
        }

        XoverChannel xoverChannel = DspSettings.XOVER_SETTINGS.getXoverChannels().get(eqChannel.getChannel());
        byte[] octTypes = new byte[]{(byte) DrawFunction.getOctType(xoverChannel.getLowFilterType(), xoverChannel.getLowFilterValue()), (byte) DrawFunction.getOctType(xoverChannel.getHighFilterType(), xoverChannel.getHighFilterValue())};
        mEqChart.setColor(eqChannel.getChannel());
        mEqChart.setPEQ(chartQ, chartGain, eqChannel.getFreq());
        mEqChart.setXover(new int[]{xoverChannel.getLowFrequency(), xoverChannel.getHighFrequency()}, octTypes);
        mEqChart.reArray();

    }

    private void updateChart(EqChannel eqChannel) {
        YzDebug.d("updateChart:");
        int qSize = eqChannel.getQ().length;
        chartQ = new float[qSize];
        chartGain = new int[qSize];
        for (int i = 0; i < qSize; i++) {
            chartQ[i] = eqChannel.getQ()[i] / 100f;
            chartGain[i] = (eqChannel.getGain()[i] + SettingConstants.GAIN_MIN) / 10;
        }

        mEqChart.resetPEQ(chartQ, chartGain, eqChannel.getFreq());
    }

    public interface CallBack {
        void onCallback(EqChannel channel, boolean isGrouping, boolean isNotSendCmd);
    }

    private void processByEqChannel(boolean isNotSendCmd, CallBack c) {
        int curChannelIndex = DspSettings.EQ_SETTINGS.getLastChannel();
        int useIndex[];
        if (DspSettings.EQ_SETTINGS.isGroup12() && (curChannelIndex == 0 || curChannelIndex == 1)) {
            useIndex = Grouping.GROUP1_2.getGroupIndexs();
        } else if (DspSettings.EQ_SETTINGS.isGroup34() && (curChannelIndex == 2 || curChannelIndex == 3)) {
            useIndex = Grouping.GROUP3_4.getGroupIndexs();
        } else if (DspSettings.EQ_SETTINGS.isGroup56() && (curChannelIndex == 4 || curChannelIndex == 5)) {
            useIndex = Grouping.GROUP5_6.getGroupIndexs();
        } else if (DspSettings.EQ_SETTINGS.isGroup78() && (curChannelIndex == 6 || curChannelIndex == 7)) {
            useIndex = Grouping.GROUP7_8.getGroupIndexs();
        } else if (DspSettings.EQ_SETTINGS.isGroupAll()) {
            useIndex = Grouping.GROUP_ALL.getGroupIndexs();
        } else {
            useIndex = new int[]{curChannelIndex};
        }
        List<EqChannel> eqChannels = DspSettings.EQ_SETTINGS.getEqChannels();

        YzDebug.d("processByEqChannel:" + Arrays.toString(useIndex));
        boolean isGrouping = useIndex.length > 1;
        int i = 0;
        if (isGrouping) {
            for (int len = useIndex.length - 1; i < len; i++) {
                int index = useIndex[i];
                if (index >= eqChannels.size() - 1) {
                    c.onCallback(eqChannels.get(index), isGrouping, isNotSendCmd);
                    return;
                } else {
                    c.onCallback(eqChannels.get(useIndex[i]), isGrouping, true);
                }

            }
        }
        c.onCallback(eqChannels.get(useIndex[useIndex.length - 1]), isGrouping, isNotSendCmd);
    }



//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onNotifyEvent(NotifyDataEvent event) {
//        mTvRead.setText(ParseDataProtocolUtil.getReadDataIndex() + " : " + ByteUtil.bytesToHexString(event.getValue()));
//    }


    @Override
    public void onPause() {
        super.onPause();
        DataUtil.saveEqSettingsToDb();
    }
    private void updateEqItemViewEnable(boolean enable){
        if (enable){
            int curEqChannelIndex =  EQ_SETTINGS.getLastChannel();
            XoverChannel xoverChannel = null;

            EqChannel eqChannel = EQ_SETTINGS.getEqChannels().get(curEqChannelIndex);
            if(EQ_SETTINGS.isGrouped()){
                int xoverChannelIndex = -1;
                switch (curEqChannelIndex){
                    case 0:
                    case 1:
                        xoverChannelIndex = EQ_SETTINGS.getBaseCh1() == 1?0:1;
                        break;
                    case 2:
                    case 3:
                        xoverChannelIndex = EQ_SETTINGS.getBaseCh3() == 1?2:3;
                        break;
                    case 4:
                    case 5:
                        xoverChannelIndex = EQ_SETTINGS.getBaseCh5() == 1?4:5;
                        break;
                    case 6:
                    case 7:
                        xoverChannelIndex = EQ_SETTINGS.getBaseCh5() == 1?6:7;
                        break;
                }
                if(xoverChannelIndex != -1){
                    xoverChannel = XOVER_SETTINGS.getXoverChannels().get(xoverChannelIndex);
                }
            }else {
                xoverChannel = XOVER_SETTINGS.getXoverChannels().get(curEqChannelIndex);
            }
            if(xoverChannel == null){
                for (int i = 0, len = mEqItemViews.length; i < len; i++) {
                    mEqItemViews[i].setEnabled(true);
                }
            }else {
                int[] freqs = eqChannel.getFreq();
                int max = xoverChannel.getLowFrequency();
                int min = xoverChannel.getHighFrequency();

                for (int i = 0, len = mEqItemViews.length; i < len; i++) {
                    YzDebug.d("updateEqItemViewEnable:" +i +" freq:"+ freqs[i] +  " min:" + min +" max:" + max);
                    mEqItemViews[i].setEnabled(freqs[i] >= min && freqs[i]<= max);
                }
            }

        }else {
            for (int i = 0, len = mEqItemViews.length; i < len; i++) {
                mEqItemViews[i].setEnabled(false);
            }
        }
    }

    //=============================================================
    private final PbxCircleSeekBar.OnCircleSeekBarFunListener mOnCircleSeekBarFunListener = new PbxCircleSeekBar.OnCircleSeekBarFunListener() {
        @Override
        public void onFunClick(PbxCircleSeekBar bar, PbxCircleSeekBar.FunId id) {
                int value = 0,  progress = bar.getProgress();;
                switch (bar.getId()){
                    case R.id.eq_seekbar_q:
                        switch (id){
                            case FUN_ID_ADD:
                                if (progress < 1995){
                                    value = progress +  5;
                                    if (value > 1995)
                                        value = 1995;
                                }
                                break;
                            case FUN_ID_SUB:
                                if (progress > 0){
                                    value = progress -5;
                                    if (progress <  0)
                                        progress = 0;
                                }
                                break;
                        }
                        if (value != 0){
                            bar.setProgress(value);
                            mOnQSeekBarListener.onProgressChange(bar,bar.getProgress(), MotionEvent.ACTION_UP );
                        }
                        break;
                    case R.id.eq_seekbar_freq:
                        progress = FreqUtil.parseProgressToFreq(progress);
                        switch (id){
                            case FUN_ID_ADD:
                                if(progress < 20000){
                                    value = progress + 1;
                                }
                                break;
                            case FUN_ID_SUB:
                                if (progress > 20){
                                    value = progress -1 ;
                                }
                                break;
                        }
                        YzDebug.d("onFunClick value:"+ value +" pro:"+ progress);
                        if (value != 0){
                            value = FreqUtil.parseFreqToProgress(value);
                            bar.setProgress(value);
                            mOnFreqSeekBarListener.onProgressChange(bar,bar.getProgress(), MotionEvent.ACTION_UP );
                        }
                        break;

                }
        }
    };
    private final PbxCircleSeekBar.OnCircleSeekBarListener mOnQSeekBarListener = new PbxCircleSeekBar.OnCircleSeekBarListener() {
        private long mLastSendCmdTime;
        @Override
        public void onProgressChange(PbxCircleSeekBar seekBar, int progress, int action) {
            boolean sendCmd = false;
            long now = SystemClock.elapsedRealtime();
            if(action == MotionEvent.ACTION_UP || (now - mLastSendCmdTime) > 250){
                mLastSendCmdTime = now;
                sendCmd = true;
            }
            int q = DspUtil.processToQ(progress);
            mEtQ.setText(String.format("%.02f",q/100f));
            processByEqChannel(!sendCmd, (channel, isGrouping, isNotSendCmd1) -> {
                channel.setQValue(mCurrentFreqIndex, q, isGrouping, isNotSendCmd1);
            });

            EqChannel channel = DspSettings.EQ_SETTINGS.getEqChannels().get( EQ_SETTINGS.getLastChannel());
            updateChart(channel);
        }
    };
    private final PbxCircleSeekBar.OnCircleSeekBarListener mOnFreqSeekBarListener = new PbxCircleSeekBar.OnCircleSeekBarListener() {
        private long mLastSendCmdTime;
        @Override
        public void onProgressChange(PbxCircleSeekBar seekBar, int progress, int action) {
            int freq = FreqUtil.parseProgressToFreq(progress);
            mEtFreq.setText(String.valueOf(freq));
            YzDebug.d("onProgressChange:"+ progress +"   " + freq);
            mEqItemViews[mCurrentFreqIndex].setFreqValue(freq);
            boolean sendCmd = false;
            long now = SystemClock.elapsedRealtime();
            if(action == MotionEvent.ACTION_UP || (now - mLastSendCmdTime) > 250){
                mLastSendCmdTime = now;
                sendCmd = true;
            }
            processByEqChannel(!sendCmd, (channel, isGrouping, isNotSendCmd1) ->
                    channel.setFreqValue(mCurrentFreqIndex, freq, isGrouping, isNotSendCmd1));
            EqChannel channel = EQ_SETTINGS.getEqChannels().get( EQ_SETTINGS.getLastChannel());
            updateChart(channel);
        }
    };
    private final PbxOutputSelectChannelView.OnCheckChangeListener mChannelSelectListener = new PbxOutputSelectChannelView.OnCheckChangeListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onCheckChange(PbxOutputSelectChannelView view, boolean isChecked) {
            if (!isChecked)return;
            int channelIndex = findCheckedIndex(view.getChannelIndex());
            EQ_SETTINGS.setLastChannel(channelIndex);
            resetSelectedChannelStatus(channelIndex);
            mPbxOutputSelectChannelsLayout.postInvalidate();
        }
    };

    private final OnSeekBarChangeListener mOnGainSeekBarChangeListener = new OnSeekBarChangeListener(){
        private long mLastSendCmdTime;
        @Override
        public void onProgressChanged(EqChannelView seekBar, int progress) {
            boolean sendCmd = false;
            final long now = SystemClock.elapsedRealtime();
            if (now - mLastSendCmdTime > 250){
                sendCmd = true;
            }
            final int index = seekBar.getFreqIndex();
            initFreqAndQValue(seekBar.getPageIndex(),index);
            processByEqChannel(!sendCmd, (channel, isGrouping, isNotSendCmd1) -> {
                channel.setGainValue(index, progress, isGrouping, isNotSendCmd1);
            });
            if (!isUpdated) {
                int curChannelIndex = EQ_SETTINGS.getLastChannel();
                updateChart(EQ_SETTINGS.getEqChannels().get(curChannelIndex));
            }
        }

        @Override
        public void onTrackingTouch(EqChannelView seekBar, boolean isStart) {
            final int index = seekBar.getFreqIndex();
            initFreqAndQValue(0,index);
            processByEqChannel(false, (channel, isGrouping, isNotSendCmd1) -> {
                channel.setGainValue(index, seekBar.getProgress(), isGrouping, isNotSendCmd1);
            });
        }
    };
//    @OnClick({R.id.eq_page_sub, R.id.eq_page_add})
//    public void onPageAddSubClick(View view) {
//        switch (view.getId()){
//            case R.id.eq_page_add:
//                int cur = mVpChannelsPager.getCurrentItem();
//                if (++cur <= mVpChannelsPager.getChildCount()){
//                    mVpChannelsPager.setCurrentItem(cur);
//                }
//                break;
//            case R.id.eq_page_sub:
//                cur = mVpChannelsPager.getCurrentItem();
//                if (--cur >= 0){
//                    mVpChannelsPager.setCurrentItem(cur);
//                }
//                break;
//
//        }
//    }
    private XoverEditText.ValidEditTextListener2 mEditTextListener = new XoverEditText.ValidEditTextListener2() {
        @Override
        public void onValidEditTextValue(XoverEditText et) {
            switch (et.getId()){
                case R.id.pbx_eq_et_freq:
                    int freq = 0;
                    try {
                        freq = Integer.parseInt(et.getText().toString().trim());
                    }catch (Exception e){}
                    if (freq < SettingConstants.FREQ_MIN) {
                        freq = SettingConstants.FREQ_MIN;

                    } else if (freq > SettingConstants.FREQ_MAX) {
                        freq = SettingConstants.FREQ_MAX;
                    }
                    mSeekBarFreq.setProgress(FreqUtil.parseFreqToProgress(freq), true);
                    //et.setText(String.valueOf(freq));
                    mOnFreqSeekBarListener.onProgressChange(mSeekBarFreq, mSeekBarFreq.getProgress(), MotionEvent.ACTION_UP);
                    break;

                case R.id.pbx_eq_et_q:
                    int q = 0;
                    try {
                        q = (int) (Float.parseFloat(et.getText().toString().trim())*100);
                    }catch (Exception e){}
                    if(q < SettingConstants.Q_MIN){
                        q = SettingConstants.Q_MIN;
                    }else if(q > SettingConstants.Q_MAX){
                        q = SettingConstants.Q_MAX;
                    }
                    mSeekBarQ.setProgress(DspUtil.qToProcess(q));
                    mOnQSeekBarListener.onProgressChange(mSeekBarQ,mSeekBarQ.getProgress(), MotionEvent.ACTION_UP);
                    break;
            }

        }
    };
}